package com.cc.stock.serviceImpl;

import com.cc.stock.dao.DealOfDao;
import com.cc.stock.dao.DelegateDao;
import com.cc.stock.entity.Deal;
import com.cc.stock.entity.Delegate;
import com.cc.stock.service.DelegateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DelegateServiceImpl implements DelegateService {

    @Autowired
    private DelegateDao delegateDao;

    @Autowired
    private DealOfDao dealOfDao;


    public void addDelegate(int userId, String stockId, double price, int num, boolean type, String time) {
        if (!type) {
            List<Deal> list = dealOfDao.findByStock(userId, stockId);
            int available = 0;
            for (Deal item: list) {
                if (new Date().getTime() - item.getTime().getTime() >= 86400000){
                    available += item.getNum();
                }
            }
            if (num <= available){
                delegateDao.addDelegate(userId,stockId,price,num,type,time);
            }
        }else {
            delegateDao.addDelegate(userId,stockId,price,num,type,time);
        }

    }

    @Override
    public List<Delegate> findAllDelegate(int userId) {
        return delegateDao.findAllDelegate(userId);
    }

    @Override
    public void cancelDelegate(int delegateId) {
        delegateDao.cancelDelegate(delegateId);
    }
}
